﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Reflection;

using RTGS.Web.support;

namespace RTGS.Web.usercontrol
{
    public partial class searching : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindOperand();
            BindField();

        }

        private void BindOperand()
        {            
            //Dictionary<string, string> dict = new Dictionary<string, string>();
            //dict.Add(">", ">");
            //dict.Add("<", "<");
            //dict.Add("<>", "<>");
            //dict.Add(">=", ">=");
            //dict.Add("<=", "<=");
            //dict.Add("=", "=");
            //dict.Add("like", "like");

            //ddlOperand.DataSource = dict;
            //ddlOperand.DataTextField = "Value";
            //ddlOperand.DataValueField = "Key";
            //ddlOperand.DataBind();        

            List<string> list = new List<string>();

            if (!string.IsNullOrEmpty(IncludeParameter))
            {
                IncludeField = IncludeParameter.Replace(" ", "");
                string[] fields = IncludeParameter.Split(',');

                foreach (string field in fields)
                {
                    list.Add(field);
                }
            }

            list.ForEach(delegate(string field)
            {
                ddlOperand.Items.Add(new ListItem(field, field));
            });
        
        }      

        public string ClassName
        {
            get;
            set;
        }

        public string ExcludeField
        {
            set;
            get;
        }

        public string IncludeField
        {
            get;
            set;
        }

        public string AliasField
        {
            get;
            set;
        }

        public string IncludeParameter
        {
            get;
            set;
        }

        private void BindField()
        {
            ddlField.Items.Clear();

            if (string.IsNullOrEmpty(ClassName))
            {
                ddlField.Items.Insert(0, new ListItem("-- please define ClassName property --", "-1"));
                return;
            }

            Assembly assembly = Assembly.Load("RTGS.Library");
            Type[] classTypes = assembly.GetTypes();

            foreach (Type cls in classTypes)
            {
                if (cls.IsClass)
                {
                    string fullname = cls.FullName.ToLower();

                    if (fullname.Contains(ClassName.ToLower()) && fullname.Contains("field")
                        &&!fullname.Contains("systmenurights")&&!fullname.Contains("systuserrole")) // Get Field Class from Domain Object
                    {
                        PropertyInfo[] propertyInfos;
                        propertyInfos = cls.GetProperties();

                        Dictionary<string, string> fieldsDicts = new Dictionary<string, string>();

                        foreach (PropertyInfo propertyInfo in propertyInfos)
                        {
                            if (!string.IsNullOrEmpty(ExcludeField))
                            {
                                ExcludeField = ExcludeField.Replace(" ", "");
                                string[] fields = ExcludeField.Split(',');

                                List<string> lowFields = new List<string>();
                                foreach (string field in fields)
                                {
                                    var lowField = field.Replace("_", "").ToLower();
                                    lowFields.Add(lowField);
                                }

                                var lowName = propertyInfo.Name.ToLower();
                                if (!lowFields.Contains(lowName))
                                {
                                    var propValue = propertyInfo.GetValue(cls, null);
                                    var propName = propertyInfo.Name;
                                    fieldsDicts.Add(propName, propValue.ToString());
                                }
                            }
                            else
                            {
                                var propValue = propertyInfo.GetValue(cls, null);
                                var propName = propertyInfo.Name;
                                fieldsDicts.Add(propName, propValue.ToString());
                            }

                        }

                        if (!string.IsNullOrEmpty(IncludeField))
                        {
                            IncludeField = IncludeField.Replace(" ", "");
                            string[] fields = IncludeField.Split(',');

                            foreach (string field in fields)
                            {
                                var key = Helper.String.PropCaseStartLower(field).Replace("_", "");
                                var value = field;
                                fieldsDicts.Add(key, value);
                            }
                        }

                        List<string> listAlias = new List<string>();

                        if (!string.IsNullOrEmpty(AliasField))
                        {
                            //AliasField = AliasField.Replace(" ", "");
                            string[] fields = AliasField.Split(',');

                            foreach (string field in fields)
                            {
                                listAlias.Add(field);
                            }
                        }

                        fieldsDicts.OrderBy(x => x.Key);

                        ddlField.Items.Clear();
                        string aliasField = string.Empty;
                        foreach (KeyValuePair<string, string> kvp in fieldsDicts)
                        {
                            //ddlField.Items.Add(new ListItem(kvp.Key, kvp.Value));

                            foreach (string fieldAlias in listAlias)
                            {
                                string[] fieldsAlias = fieldAlias.Split('=');
                                if (fieldsAlias[0].ToString().Trim() == kvp.Key.Trim())    
                                {
                                    aliasField = fieldsAlias[1].ToString();
                                    ddlField.Items.Add(new ListItem(aliasField, kvp.Value));
                                    break;
                                }

                            }
                        }

                        ddlField.Items.Insert(0, new ListItem("-- pilih parameter --", "-1"));        

                        break;
                    }
                }
            }
        }

    }

}